package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EquipCategoryFormMap;
import com.iemes.entity.HolidayprojectFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipCategoryMapper;
import com.iemes.mapper.equip.HolidayprojectMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("equip")
@Controller
public class HolidayprojectController extends BaseController{
	
	@Inject
	private HolidayprojectMapper holidayproject;
	
	@Inject
	private EquipCategoryMapper categorymapper;
	
	@RequestMapping(value = "equip_holiday", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		//种类表信息
		List<EquipCategoryFormMap> categoryFormMap = categorymapper.findByWhere(new EquipCategoryFormMap());
		List<HolidayprojectFormMap> holidayprojectFormMap = holidayproject
				.findByWhere(new HolidayprojectFormMap());
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("categoryFormMap", categoryFormMap);
		model.addAttribute("equipbom", holidayprojectFormMap);
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/holidayproject/equipbom_list";	
	}
	
	@ResponseBody
	@RequestMapping("equip_holiByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		HolidayprojectFormMap holidayprojectFormMap = getFormMap(HolidayprojectFormMap.class);
		if (holidayprojectFormMap.get("status")==null) {
			holidayprojectFormMap.put("status", 1);
		}
		List<Map<String,Object>> equipHolis = holidayproject.getEquipSearch(holidayprojectFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipHolis);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}
	@RequestMapping("equip_holiAdd")
	public String addUI(Model model) throws Exception {	
		return Common.BACKGROUND_PATH + "/equip/holidayproject/equipbom_add";
	}

	@ResponseBody
	@RequestMapping("equipbom_holiaddEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "设备节假日设备维修计划表", methods = "设备-新增维修计划表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		HolidayprojectFormMap holidayprojectFormMap = getFormMap(HolidayprojectFormMap.class);
		if (holidayprojectFormMap.size()==0 || holidayprojectFormMap==null) {
			return "false";
		}
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		holidayprojectFormMap.put("date", sdf.format(date));
		holidayprojectFormMap.put("fillProject", userFormMap.get("id"));
		holidayprojectFormMap.put("status",1);
		holidayproject.addEntity(holidayprojectFormMap);
		return "success";
	}
	@RequestMapping("holi_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		HolidayprojectFormMap holidayprojectFormMap = holidayproject.findbyFrist("id", id,
				HolidayprojectFormMap.class);
		model.addAttribute("holidayp", holidayprojectFormMap);
		return Common.BACKGROUND_PATH + "/equip/holidayproject/equipbom_edit";
	}

	@ResponseBody
	//@Transactional(readOnly=false)
	@RequestMapping(value = "holi_editEntity")
	@SystemLog(module = "设备节假日设备维修计划表", methods = "设备-修改维修计划表")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		HolidayprojectFormMap holidayprojectFormMap =getFormMap(HolidayprojectFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		holidayprojectFormMap.put("date", sdf.format(date));
		holidayprojectFormMap.put("fillResult",userFormMap.get("id"));
		holidayproject.editEntity(holidayprojectFormMap);
		return "success";
	}
		
	@ResponseBody
	@RequestMapping(value="holi_deleteEntity")
	@SystemLog(module="设备节假日设备维修计划表",methods="设备-删除维修计划表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {	
		HolidayprojectFormMap holidayprojectFormMap =getFormMap(HolidayprojectFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");	
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			holidayprojectFormMap.put("date", sdf.format(date));
			holidayprojectFormMap.put("fillProject", userFormMap.get("id"));
			holidayprojectFormMap.set("status", 2);
			holidayprojectFormMap.put("id", str[i]);
			holidayproject.editEntity(holidayprojectFormMap);
		}	
		return "success";
	}	
}
